import api from '../../api';
import _ from 'lodash';

export default {
  namespace: 'SearchAndSelectM',
  state: {},
  effects: {
    // 根据查询条件,获取下拉列表数据
    *getSelectData({ payload, callback }, { call, put, select }) {
      const args = _.cloneDeep(payload);
      delete args.type;
      delete args.value;
      const value = payload.value ?? '';
      // 处理入参
      switch (payload.type) {
        // 产品管理
        case 'productManage':
          args.productName = payload.value;
          break;
        // 非标准化债权类资产
        case 'nonstandardzc':
          if (value != '') {
            args.code = payload.value;
          }
          break;
        // 非标准化债权类投资
        case 'nonstandardtz':
          if (value != '') {
            args.code = payload.value;
          }
          break;
        // 资产管理计划维护
        case 'zgtzptPlan':
          if (value != '') {
            args.value = payload.value;
          }
          break;
        // 理财直融
        case 'finaFlocking':
          if (value != '') {
            args.current = 1;
            args.pageSize = 10;
            args.search_LIKE_secucode = payload.value;
            delete args.currentPage;
            delete args.numInPage;
          } else {
            args.current = 1;
            args.pageSize = 10;
            delete args.currentPage;
            delete args.numInPage;
          }
          break;
      }
      // 后台返回前100条数据
      const res = yield call(api.getProductNameData[payload.type], args);

      if (res && res.code == 0) {
        const dataSource = [];
        const dataSourceLength = res.data?.dataSource?.length;
        for (let i = 0; i < dataSourceLength; i++) {
          if (i >= 100) break; // 取前100条数据
          const item = res.data.dataSource[i];
          // 处理数据
          switch (payload.type) {
            // 产品管理 估值代码-产品简称的展示模式，如果估值代码未填，采用内部代码替代。
            case 'productManage':
              {
                item.value = item.id;
                item.title = `${item.code || item.gzCode} - ${item.productSimpleName}`;
              }
              break;
            // 非标准化债权类资产 代码-简称
            case 'nonstandardzc':
              {
                item.value = item.id;
                item.title = `${item.tradingcode} - ${item.secuabbr}`;
              }
              break;
            // 非标准化债权类投资 代码-简称
            case 'nonstandardtz':
              {
                item.value = item.id;
                item.title = `${item.tradingcode} - ${item.secuabbr}`;
              }
              break;
            // 资产管理计划维护 代码-全称
            case 'zgtzptPlan':
              {
                item.value = item.id;
                item.title = `${item.tradingcode} - ${item.tradingname}`;
              }
              break;
            // 理财直融
            case 'finaFlocking':
              {
                item.value = item.id;
                item.title = `${item.secucode} - ${item.secuabbr}`;
              }
              break;
          }

          dataSource.push(item);
        }
        callback(dataSource);
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
